package top.yxqz.util;

import org.apache.ibatis.io.Resources;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.apache.ibatis.session.SqlSessionFactoryBuilder;
import top.yxqz.mapper.UserMapper;
import top.yxqz.pojo.User;

import java.io.IOException;
import java.io.InputStream;

/**
 * @author: 余小小
 * @date: 2023/3/7
 * @description：sqlSession工具类 是static修饰的：可以通过类名直接打点调用
 * public修饰的
 */
public class SqlSessionUtils {
    public static SqlSession getSqlSession() {
        InputStream resourceAsStream = null;
        SqlSession sqlSession = null;
        try {
            resourceAsStream = Resources.getResourceAsStream("mybatis-config.xml");
            SqlSessionFactoryBuilder sqlSessionFactoryBuilder = new SqlSessionFactoryBuilder();
            SqlSessionFactory sqlSessionFactory = sqlSessionFactoryBuilder.build(resourceAsStream);
//        mysql默认的事务是不会自动提交的
            sqlSession = sqlSessionFactory.openSession(true);
        } catch (IOException e) {
            throw new RuntimeException(e);
        }

//        sqlSession.commit(); //手动提交 默认是false
        return sqlSession;
    }
}
